package com.example.pkmedical.Activity.usermsg;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pkmedical.Activity.model.UserMessage;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserMessageActivity extends BaseActivity {
    @BindView(R.id.user_name_edit)
    EditText userNameEdit;
    @BindView(R.id.user_age_edit)
    EditText userAgeEdit;
    @BindView(R.id.user_date_edit)
    EditText userDateEdit;
    @BindView(R.id.user_sex_edit)
    EditText userSexEdit;
    @BindView(R.id.user_height_edit)
    EditText userHeightEdit;
    @BindView(R.id.user_weight_edit)
    EditText userWeightEdit;
    @BindView(R.id.user_ok_bt)
    Button userOkBt;
    @BindView(R.id.user_ok_cancle)
    Button userOkCancle;
    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_usermsg);
        ButterKnife.bind(this);
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void initData() {
        userNameEdit.setEnabled(false);
        userSexEdit.setEnabled(false);
        userAgeEdit.setEnabled(false);
        userHeightEdit.setEnabled(false);
        userDateEdit.setEnabled(false);
        userWeightEdit.setEnabled(false);

        getData();
    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_usermsg);
        mainHouseText.setText(R.string.title_user);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.USER_MESSAGE) {
            getData();
        }
    }

    @OnClick({R.id.user_ok_bt, R.id.user_ok_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_ok_bt:
                startActivity(new Intent(this, NewPatientActivity.class));
                break;
            case R.id.user_ok_cancle:
                UserMessageActivity.this.finish();
                break;
        }
    }

    private void getData() {
        List<UserMessage> list = LitePal.findAll(UserMessage.class);
        if (list.size() > 0) {
            List<UserMessage> userMessage = LitePal.findAll(UserMessage.class);
            userNameEdit.setText(userMessage.get(0).getUserName());
            userSexEdit.setText(userMessage.get(0).getUserSex());
            userAgeEdit.setText(userMessage.get(0).getUserAge());
            userHeightEdit.setText(userMessage.get(0).getUserHeight());
            userDateEdit.setText(userMessage.get(0).getInDate());
            userWeightEdit.setText(userMessage.get(0).getUserWeight());
        }
    }

    @Override
    public void showDialog(String... msg) {

    }
}
